<?php
$queueLineLinks = $screen->populateQueueLineLinks('showMakeQueueList');
$activeQueueLine = $screen->getActiveQueueLine();

if (isset($_POST['MakeQueueFormModel'])) {
    $screen->makeQueueFormModel->attributes = $_POST['MakeQueueFormModel'];
}
$makeQueueFormModel = $screen->makeQueueFormModel;
?>
<!-- show menu of queuelines -->
<div id="queueLineLinks" class="pagination">
    <ul>
        <?php
        if ($queueLineLinks == NULL) {
            echo 'Not find QueueLine!';
            // yii::app()->end();
        }
        foreach ($queueLineLinks as $link) {
            $active = $link['active'] == TRUE ? 'class="active"' : '';
            $qLine = BAQQueueLine::find(array(BAQQueueLine::key_QueueLineId => $link['id']));
            $transactions = $qLine->getTransactions(array('makeQueueTime' => time()));
            if (count($transactions) > 0)
                echo '<li ' . $active . '>' . $link['ajaxLink'] . '</li>';
        }
        ?>    
    </ul>
</div>
<!-- show list of makequeue -->
<div id="showMakeQueueList">
    <?php echo CHtml::beginForm(); ?>
    <?php
    if ($activeQueueLine != NULL) {
        $transactions = $activeQueueLine->getTransactions(array('makeQueueTime' => time()));
        if ($transactions == NULL)
            echo 'not find transaction!';
        foreach ($transactions as $transaction) {
            $resource = BAQResource::find(array(BAQResource::Key_Resource_Id => $transaction->resourceId));
            $makeQueueStatus = $screen->checkMakeQueueStatus($transaction->id);
            $queueMessages = isset($makeQueueStatus['queueMessages']) ? $makeQueueStatus['queueMessages'] : array();
            $maxQueueNo = $resource->maxQueueNumber;
            if (!(is_numeric($maxQueueNo) && intval($maxQueueNo)) > 0)
                $maxQueueNo = -1;
            ?>
            <div class="row" style="margin-top: -15px;margin-left: 1px">                
                <?php if (isset($makeQueueStatus['queueNumber'])) { ?>
                    <div class="span2 text-info"><h5>Your Queue:</h5></div>
                    <div class="span1 text-success"><h4><?php echo $makeQueueStatus['queueNumber']; ?></h4></div>
                    <div class="span1 text-warning"><h5>Status:</h5></div>
                    <div class="span2 text-info"><h5><?php echo $makeQueueStatus['processQueueStatus']; ?></h5></div>
                <?php } ?>
            </div>
            <div class="well"><?php echo $screen->populateMakeQueueLink($makeQueueStatus); ?></div>
            <div class="well">
                <div>
                    <fieldset>                    
                        <label class="text-info">Your message:</label>
                        <?php $makeQueueFormModel->makeQueueMessage = NULL;?>
                        <?php echo CHtml::activeTextArea($makeQueueFormModel, 'makeQueueMessage', array('placeholder' => 'Type message to queue handler...', 'cols' => 30)); ?>
                    </fieldset>
                </div>
                <div>
                    <?php
                    foreach ($queueMessages as $message) {
                        $user = new BUser($message['senderId']);
                        $time = date(DateTime::ATOM, $message['sendTime']);
                        ?>
                        <address>
                            <strong>From:<?php echo $user->getLoginId(); ?></strong>--@:<?php echo $time; ?><br>
                            <?php echo $message['message']; ?>                                
                        </address>
                    <?php } ?>
                </div>
            </div>
            <table class="table table-striped">
                <tr><td class="text-info">Queue For What:</td><td><?php echo $resource->name; ?></td></tr>
                <tr><td class="text-info">Queue Start Time:</td><td><?php echo date(DATE_ATOM, $transaction->startTime); ?></td></tr>
                <tr><td class="text-info">Queue End Time:</td><td><?php echo date(DATE_ATOM, $transaction->endTime); ?></td></tr>
                <?php if ($maxQueueNo > 0) { ?>
                    <tr><td class="text-info">Max Queue No.:</td><td><?php echo $maxQueueNo; ?></td></tr>
                <?php } ?>
                <tr><td class="text-info">Total Processed Queues:</td><td><?php echo $makeQueueStatus['processedQueue']; ?></td></tr>
                <tr><td class="text-info">Pending Queues Before Your:</td><td><?php echo $makeQueueStatus['pendingQueue']; ?></td></tr>     
                <?php if (isset($makeQueueStatus['estimateWaitingTime'])) { ?>
                    <tr><td class="text-info">Estimate waiting time:</td><td><?php echo $makeQueueStatus['estimateWaitingTime']; ?> </td></tr> 
                <?php } ?>
                <?php if (isset($makeQueueStatus['queueNumber']) && isset($makeQueueStatus['makeQueueTime'])) { ?>
                    <tr><td class="text-info">Make queue time:</td><td><?php echo $makeQueueStatus['makeQueueTime']; ?> </td></tr> 
                <?php } ?>
                <?php if (isset($makeQueueStatus['queueNumber']) && isset($makeQueueStatus['cancelQueueTime'])) { ?>
                    <tr><td class="text-info">Cancel queue time:</td><td><?php echo $makeQueueStatus['cancelQueueTime']; ?> </td></tr> 
                <?php } ?>
            </table>



            <?php
        }
    }
    ?>
    <?php echo CHtml::endForm(); ?>
</div>
